﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DTBuilderLib;
using IndicoInterface;
using System.Xml.Serialization;

namespace t_SSI
{
    public class Utilities
    {
        public static DirectoryInfo GetSLTalk()
        {
            ///
            /// Sadly, we have to correctly render a whole bloody meeting
            /// 

            /// Render a session that we have downloaded.
            JobDownloader jd = new JobDownloader();
            AgendaInfo a = new AgendaInfo(33078);
            var data = jd.DownloadMeeting(a.GetNormalizedConferenceData());

            JPEGRender r = new JPEGRender();
            var renderedSlides = r.RenderMeeting(data);

            DeepZoomBuilder bld = new DeepZoomBuilder();
            bld.BuildDirectory += "\\" + "SetWebsiteBuilder";

            /// Don't run visualizer if we don't need it - way faster!
            if (!bld.MeetingAlreadyRendered("meeting"))
            {

                LayoutBuilder layout = new LayoutBuilder();
                var v = layout.BuildVisualTreeForAMeeting(data, renderedSlides);

                bld.RenderVisual("meeting", v);
            }

            WebSiteBuilder.CreateWebSite(new DirectoryInfo(bld.BuildDirectory), data.Title);

            return new DirectoryInfo(bld.BuildDirectory);
        }

        /// <summary>
        /// Holds onto the account info. Keep secret!!
        /// </summary>
        [Serializable]
        public class accountInfo
        {
            public string id;
            public string key;
        }

        static XmlSerializer _accountSerlizer;

        /// <summary>
        /// Get the account info from a file. If the file isn't there, then create a blank one and wait. :-)
        /// </summary>
        /// <returns></returns>
        static public accountInfo GetAccountInfo()
        {
            string filename = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\livestreamingaccount.xml";

            if (_accountSerlizer == null)
            {
                _accountSerlizer = new XmlSerializer(typeof(accountInfo));
            }

            if (!File.Exists(filename))
            {
                /// Too bad -- this is going to fail! Write a dummy so the user can get started.

                using (TextWriter wr = File.CreateText(filename))
                {
                    accountInfo info = new accountInfo() { id = "FillIDHere", key = "Fill Key HERE" };
                    _accountSerlizer.Serialize(wr, info);
                    wr.Close();
                }

                throw new ArgumentException("Account info not found in MyDocuments/livestreamingaccount.xml - dummy created, please fill in");
            }

            using (TextReader wr = File.OpenText(filename))
            {
                accountInfo ac = _accountSerlizer.Deserialize(wr) as accountInfo;
                wr.Close();
                return ac;
            }
        }
    }
}
